
local util                             = require("util")
local skynet                           = require("skynet")
local Timer                            = require("Timer")
local FSM                              = require("FSM")
local reason                           = require("reason")
local constant                         = require("constant")
local error_code                       = require "error_code"

local DeskBase = import("DeskBase")
local Desk = class("Desk",DeskBase)

--全局变量本地化
local mix_array = mix_array
local table_clone = table_clone
local math_floor = math.floor
local math_ceil = math.ceil
local math_random = math.random
local table_insert = table.insert
local math_abs = math.abs
local table_remove = table.remove
local math_min = math.min
local string_split = string.split
local table_len = table.len

--构造函数
function Desk:ctor()
    self.super:ctor()
end

----------------------------------------------------------------------------------------------
---------游戏
---------------------------------------------------------------------------------------------- 
function Desk:start()
    dbglog("___启动红包扫雷游戏___")
    self.super.start(self) --基础的对象里在里面有初始化的了
    --数据初始化
    self:init()
    self.service_closed = nil --服务已关闭
    --状态时间
    self.state_time = {   
        rest = 1,          --等待开始 
        startgame = 15,      --游戏时间 
        waitover = 1,       --等待结束时间  
        gameover = 3,  --结算时间
    }
    --游戏状态
    --state1 --eventName --state2 --callback
    self.state_list = { 
        {"rest",        "to_startgame",             "startgame",            function() self:onStart() end },

        {"startgame",       "to_waitover",         "waitover",              function() self:onWaitOver() end },  
        {"waitover",    "to_gameover",           "gameover",                function() self:onGameOver() end},
        {"gameover",    "to_rest",              "rest",                     function() self:onRest() end },
    }
    self.fsm = FSM.new(self.state_list) --状态机
    self.timer = Timer.new() --定时器

    dbglog("___起动机器人_1__")
    --起动机器人
    self.robot_manager:start()
    dbglog("___游戏桌子起完成_____")

    --启动状态机
    self.fsm:set("gameover")
    self:changeState("to_rest")
    dbglog("hbsl load robot")
    self.robot_manager:loadRobot() --机器人加载
    --发红包策略
    self:robot_fhb_strategy()
    return true
end

--初始化
function Desk:init()
    self.qhb_palyer_list = {}    --抢红包列表
    self.hb_list = {}            --待抢红包列表
    self.hb_coins = 0            --当前红包金额
    self.sender = 0              --当前发红包者
    self.hb_left_coins = 0       --当前红包剩余金额
    self.lei_number = 0          --当前雷号
    self.lei_count = 0           --当局中雷数
    self.open_count = 0             --红包可拆次数
    self.left_count = 0      --当前红包剩余可拆次数
    self.left_hb_count = 0      --当前红包剩余可拆次数 用于计算红包金额
    self.player_record_map = {}       --战绩列表
    self.last_player_rob_count = 0         --上一局抢红包玩家个数
    self.sender_result_coins = 0
    self.hb_id = 0
    self.system_money_cost = 0
    self.system_money_cost_a = 0
    self.hb_index = 1               --红包id自增索引
    self.group_index = 1            --续发队伍组索引
    self.last_lei_number = {}        --上次红包雷号
    self.last_hb_coins = {}          -- 上次红包金额
    self.autoSendPacketList = {}     --自动发红包列表
    self.is_robot = true        --当前发红包者是否机器人
    self.start_store_result = 0   -- 开局起始前大盘控制
    self.start_personal_uid = 0   -- 开局起始前被控者 
    self.robot_lei_count = 0  -- 控制机器人中雷数
    self.player_lei_count = 0  -- 控制玩家中雷数
    self.robot_rob_index = {}

    self.back_coins = 0 -- 自己抢自己返回金币

        ------------牌局记录-------------- 
    self.player_list = {} --玩家列表
    self.start_time = 0 --开始时间
    self.end_time = 0 --结束时间
    self.total_num = 0 --参与人数
    self.loser_list = {} --输的玩家
    self.winner_list = {} --赢的玩家   
    self.win_free_list = {} --税收列表

end

--红包id自增索引 自增
function Desk:add_hb_index()
    self.hb_index = self.hb_index + 1
    if self.hb_index > 99999999 then
        self.hb_index = 1
    end
end

--红包id自增索引 自增
function Desk:add_group_index()
    self.group_index = self.group_index + 1
    if self.group_index > 99999999 then
        self.group_index = 1
    end
end

--重置
function Desk:clear_room()
    self.hb_coins = 0            --当前红包金额
    self.sender = 0              --当前发红包者
    self.hb_left_coins = 0       --当前红包剩余金额
    self.lei_number = 0          --当前雷号 
    self.qhb_palyer_list = {}    --抢红包列表  
    self.sender_result_coins = 0 
    self.hb_id = 0
    self.lei_count = 0 
    self.system_money_cost = 0
    self.is_robot = true        --当前发红包者是否机器人
    self.start_store_result = 0   -- 开局起始前大盘控制
    self.robot_lei_count = 0  -- 控制机器人中雷数
    self.player_lei_count = 0  -- 控制玩家中雷数
    self.robot_rob_index = {}
    self.left_hb_count = 0      --当前红包剩余可拆次数 用于计算红包金额
    self.back_coins = 0 -- 自己抢自己返回金币
    self.system_money_cost_a = 0
        ------------牌局记录-------------- 
    self.player_list = {} --玩家列表
    self.start_time = 0 --开始时间
    self.end_time = 0   --结束时间
    self.total_num = 0 --参与人数
    self.loser_list = {} --输的玩家
    self.winner_list = {} --赢的玩家    
    self.win_free_list = {} --税收列表
end


--机器人发红包策略
function Desk:robot_fhb_strategy()
    local room_data = self.room_config:getRoomData()
    local robot_data = self.room_config:getRobotData()
    local min_coins = room_data.hb_coins_range[1] 
    local robot_list = self.robot_manager:getRobotsList(min_coins)
    local robot_data = self.room_config:getRobotData()

    if not robot_data then return end

    local hb_range = robot_data.hb_range or {}
    local hb_count 
    local player_count = self.user_manager:getRealUserNum()
    for _,v in pairs(hb_range) do
        if player_count >= v[1][1] and player_count <= v[1][2] then   
            dbglog(v[2])
            hb_count = math_random(v[2][1],v[2][2])
        end
    end
    hb_count = hb_count or 2
    --待发红包数目
    local wait_fhb_count = 0
    for _,robot in pairs(robot_list) do
        if robot:isfhb() then 
            wait_fhb_count = wait_fhb_count + 1
        end
    end

    local count = hb_count - #self.hb_list - wait_fhb_count 
    count = count > 0 and count or 0

    local temp = {}
    for i,robot in pairs(robot_list) do
        if not robot:isfhb() then 
            table_insert(temp,i)
        end
    end
    -- dbglog("hbsl count:",count,#temp,hb_count,#self.hb_list,wait_fhb_count)
    for i = 1,count do
        if not next(temp) then break end
        local index = table_remove(temp,math_random(#temp))
        dbglog("hbsl index :",index)
        robot_list[index]:setFhb(room_data.time)
    end
end

--机器人抢红包策略
function Desk:robot_qhb_strategy()
    local room_data = self.room_config:getRoomData()
    local robot_data = self.room_config:getRobotData()
    local min_coins = self.hb_coins*room_data.hb_times
    local robot_list = self.robot_manager:getRobotsList(min_coins)

    local temp = {}

    for i,v in pairs(robot_list) do
        table_insert(temp,i)
    end

    --秒抢概率
    local seckill_rate = robot_data.seckill_rate or 0
    if math_random(1,100) <= seckill_rate then
        local tb_time = string_split(robot_data.seckill_time_range or "2,3" , ',') 
        for i = 1,self.open_count do
            if not next(temp) then break end
            local index = table_remove(temp,math_random(#temp))
            robot_list[index]:setQhb(3,nil,tb_time)
        end  
        dbglog("seckill",seckill_rate,tb_time)
        return
    end

    local count_one = math_floor((self.open_count - self.last_player_rob_count)/2) - 2 + math_random(1,3)
    if count_one < 0 then count_one = 1 end 
    local count_two = self.open_count - count_one 
    if count_two <= 0 then count_two = 1 end
    if math_random(1,100) < (robot_data.robbed_all_rate or 0) then
        count_two = count_two + 2
    else 
        count_two = math_random(count_two)
    end

    for i = 1,count_one do
        if not next(temp) then break end
        local index = table_remove(temp,math_random(#temp))
        robot_list[index]:setQhb(1,room_data.time)
    end  

    for i = 1,count_two do
        if not next(temp) then break end
        local index = table_remove(temp,math_random(#temp))
        robot_list[index]:setQhb(2,room_data.time)
    end  
end

--等待开始
function Desk:onRest()
    dbglog("hbsl onRest")   
    self:ntRest()
    self:removeDisconnect() --删除离线的玩家
        --清理房间数据
    self.room_config:reload() --配置加载    
    self:checkServiceClose() --每局休息时检查是否已关服 
    self.robot_manager:loadRobot() --机器人加载     
    local callback = function ( ... )  
        self.room_config:reload() --配置加载  
        self:checkServiceClose() --每局休息时检查是否已关服 
        print("__________________rest__", self:getState())
        if #self.hb_list ~= 0 and self:getState() == "rest" then   
            self:changeState("to_startgame") 
            return false   
        end   
        return true
    end
    self:setTimer(self.state_time.rest, callback,true)
end


--开始游戏
function Desk:onStart()
    if #self.hb_list == 0 then
        self:changeState("to_rest")
        return
    end

    --取出一个待抢红包
    local info = table_remove(self.hb_list,1)
    self.lei_number = info.lei_number
    self.sender = info.uid
    self.hb_coins = info.coins
    self.hb_left_coins = info.coins  
    self.lei_number = info.lei_number
    self.left_count = info.count
    self.left_hb_count = info.count    
    self.open_count = info.count
    self.is_robot = info.is_robot

    local user = self.user_manager:getUser(self.sender)  
    user:setBeted(true)
        --发红包策略
    self:robot_fhb_strategy()
    self.hb_id = info.id
    self.total_num = 1
    dbglog("self.hb_left_coins:",self.hb_left_coins)
    -- --添加记录
    -- self.player_record_map[uid] = self.player_record_map[uid] or {}
    -- self.player_record_map[uid].all_hb_coins = (self.player_record_map[uid].all_hb_coins or 0) + info.coins
    local info = {
        uid = info.uid,
        rob_coins = 0,
        is_lei = false,
        result_coins = 0,  
        fee_coins = 0,
        is_robot = self.is_robot,  
    }
    table_insert(self.player_list,info)

    dbglog("hbsl onStart sender:",self.sender)
    --设置在线玩家开始
    self.user_manager:setAllUserValue("playing", true)
    local room_data = self.room_config:getRoomData()
    self.state_time.startgame = room_data.time
    -- dbglog("room_data.time",room_data.time)
    self:ntStart()
    --起始大盘控制
    self.start_store_result,self.start_personal_uid = self.store_manager:getSystemStoreResultHBSL({self.sender}, true)
    dbglog("self.start_store_result:",self.start_store_result,"self.start_personal_uid",self.start_personal_uid)
    self:cal_lei_count()
    --抢红包策略
    self:robot_qhb_strategy()
    --开始游戏
    self.start_time = os.time() 

    user:addCurBet(self.hb_coins)
    local callback = function ()      
        self:changeState("to_waitover")
    end
    self:setTimer(self.state_time.startgame, callback)
end

--大盘控制预先设定中雷数
function Desk:cal_lei_count()
    if self.is_robot then
        if self.start_store_result == constant.SYSTEM_STORE_LOSE or self.start_store_result == constant.AWARD_STORE_LOSE then

        elseif self.start_store_result == constant.SYSTEM_STORE_WIN then
            if math_random(1,100) <= 70 then
                self.player_lei_count = 1
            else
                self.player_lei_count = 2
            end 
        end
    else
        if self.start_store_result == constant.SYSTEM_STORE_LOSE or self.start_store_result == constant.AWARD_STORE_LOSE  or self.start_store_result == constant.PERSONAL_STORE_WIN  then
           if math_random(1,100) <= 70 then
                self.robot_lei_count = 1
            else
                self.robot_lei_count = 2
            end  
        elseif self.start_store_result == constant.SYSTEM_STORE_WIN then

        end
    end
end

--等待结束
function Desk:onWaitOver()
    dbglog("onWaitOver")
    self:ntWaitGameOver()
    local callback = function ()      
        self:changeState("to_gameover")
    end
    self:setTimer(self.state_time.waitover, callback)
end

--游戏结束
function Desk:onGameOver()
    self:audit_robot()
    dbglog("hbsl onGameOver")
    self.last_player_rob_count = 0
    self.end_time = os.time()
    local room_data = self.room_config:getRoomData()
    local rob_result = {}
    local fee_coins = 0
    local user = self.user_manager:getUser(self.sender)  
    self.sender_result_coins = self.hb_left_coins + self.lei_count*room_data.hb_times*self.hb_coins - self.hb_coins + self.back_coins

    if self.hb_left_coins > 0 then
        self:addChip(self.sender,self.hb_left_coins + self.back_coins,reason.QHB_BACK)
    end

    if self.lei_count > 0 then
        local ret = self:addChip(self.sender,self.lei_count*room_data.hb_times*self.hb_coins,reason.WIN_COIN)
        if not ret then
            --加钱失败
            errlog(self.sender, '红包扫雷加钱失败', coins)
        end   
    end

    if self.sender_result_coins > 0 then                 
        --服务费记录
        -- fee_coins = math_floor(self.lei_count*room_data.hb_times*self.hb_coins * room_data.cost/100)
        fee_coins = math_floor(self.sender_result_coins * room_data.cost/100)
        self:reduceChip(self.sender,fee_coins,reason.PAY_FEE)
        self:addFreeChip(self.sender, fee_coins, reason.PAY_FEE)
        self.win_free_list[self.sender] = self.win_free_list[self.sender] or 0
        self.win_free_list[self.sender] = self.win_free_list[self.sender] + fee_coins
        --对局广播
        self:sendRealHorseMessage(self.sender,self.sender_result_coins - fee_coins,constant.BROADCAST_IDS.BIDS_HBSL)    
    end
    self:checkCaiShen(user, self.sender_result_coins - fee_coins)
    --这里先通知每个人自身金币发生变化
    self:ntMoneyChangeToUser(self.sender)        
    self:ntMoneyChangeToHall(self.sender, user:getCoins(), constant.MONEY_CHANGED_REASON.HBSL_SEND)  

    if not self.is_robot then
        --增加流水
        self:winLoseWaterHBSL(user, math_abs(self.sender_result_coins), self.sender_result_coins,fee_coins,self.hb_coins)
    end  
    if not self.is_robot then
        --更新库存 
        self:updateStoreEX(self.start_store_result,-self.sender_result_coins,self.sender_result_coins,self.start_personal_uid)
    end
    --结算
    for _,info in pairs(self.qhb_palyer_list) do
        local user = self.user_manager:getUser(info.uid)

        --真实玩家数量
        if not user:getIsRobot() then
            self.last_player_rob_count = self.last_player_rob_count + 1
        end
        
        local item = {
            uid = info.uid,
            is_thunder = info.is_thunder,
            result_coins = info.result_coins,
            curr_coins = user:getCoins(),
            rob_coins = info.coins 
        }
        table_insert(rob_result,item)
        --这里先通知每个人自身金币发生变化    
        self:ntMoneyChangeToUser(info.uid)
    end

    local game_result = {
        rob_result = rob_result,
        sender_result_coins = self.sender_result_coins,
        sender_id = self.sender,
    }


    
    local sender_user = self.user_manager:getUser(self.sender)
    sender_user:setCurBet(0)
    --玩家发红包
    if not self.is_robot then
        self.system_money_cost_a = math_floor(self.system_money_cost_a - fee_coins)
        --把输赢发送到好友服
        self:sendLoseWinToFriendServer( - self.sender_result_coins + fee_coins)

        local bet_coins = self.hb_coins - self.hb_left_coins
        local produce = self.lei_count*room_data.hb_times*self.hb_coins + self.back_coins
        local win_lose = self.sender_result_coins - fee_coins 
        local free = fee_coins
        sender_user:addProduce(produce)
        self:sendSummary(self.sender, bet_coins, produce, win_lose, free)  
        sender_user:addTotalBet(self.hb_coins - self.hb_left_coins) --玩家在桌子内的投入
    end
    dbglog(tostring_r(game_result))
    self.user_manager:setAllUserValue("playing", false) 

    --发红包记录
    self:addFhbRecord(self.sender,self.hb_coins,self.lei_count,self.sender_result_coins,fee_coins,self.is_robot)
    self:writeCardRecord()
    self:UpdateAutoSendPacket(self.sender)
    local callback = function ()
        --休息
        self:clear_room()        
        self:changeState("to_rest")
    end
    self:setTimer(self.state_time.gameover, callback)

    local user_list = self.user_manager:getUserList()
    for uid, user in pairs(user_list) do 
        user:setPlaying(false)
        user:setBeted(false)     
       -- dbglog("setBeted false",uid)   
    end

    self:ntGameOver({game_result = game_result,left_time = self.state_time.gameover})
end



-------------------------------------------------------------------------------
-- callback
-------------------------------------------------------------------------------
--玩家进入消息回调 
function Desk:enterReqCallback(uid, fd, msg)
    local res = self:getEnterInfo() --桌子状态信息

    return res
end

--离线回调
function Desk:disconnectCallback(uid)
    self.last_lei_number[uid] = nil 
    self.last_hb_coins[uid] = nil
    self.autoSendPacketList[uid] = nil 
    self:delPacket(uid)
    return true
end

--离开桌子
function Desk:leaveReqCallback(uid)
    --站起
    --从user_manager中删除
    --发送桌子信息变化到zoneserver
    self.autoSendPacketList[uid] = nil 
    self.last_lei_number[uid] = nil 
    self.last_hb_coins[uid] = nil 
    self:delPacket(uid)
    return true
end

-------------------------------------------------------------------------------
-- 推送消息
-------------------------------------------------------------------------------
function Desk:ntRest()
    self:sendToAllUser('hbsl.NTF_WAIT_START',{
        left_time = self.state_time.rest,
    })  
end

function Desk:ntStart()
    self:sendToAllUser('hbsl.NTF_START',{
        left_time = self.state_time.startgame,
        packet_info = {
            sender = self:getPlayerInfo(self.sender),
            coins = self.hb_coins,
            thunder_number = self.lei_number,
            count = self.open_count,
            left_count = self.left_count,  
        },
    })
end

function Desk:ntGameOver(data)  
    self:sendToAllUser('hbsl.NTF_GAMEOVER', data)
end

function Desk:ntWaitGameOver()
      self:sendToAllUser('hbsl.NTF_WAIT_GAMEOVER',{
        left_time = self.state_time.rest,
    })   
end

-------------------------------------------------------------------------------
-- 功能函数
-------------------------------------------------------------------------------

--玩家信息
function Desk:getPlayerInfo(uid)
    local user = self.user_manager:getUser(uid)
    if not user then
        errlog("user nil uid:",uid)
        return {}
    end
    local user_info = user:getClientInfo()
    local player_info = {
        uid = uid,
        name = user_info.name,
        coins = user_info.coins,
        icon = user_info.icon,
        vip_level = user_info.vip_level,
        icon_border = user_info.icon_border,  
    }
    return player_info
end

--玩家详细信息
function Desk:getPlayerAllInfo(uid)
    local user = self.user_manager:getUser(uid)
    
    if not user then
        return {}
    end
    local user_info = user:getClientInfo()
    local player_info = {
        uid = uid,
        name = user_info.name,
        coins = user_info.coins,
        icon = user_info.icon,
        vip_level = user_info.vip_level,
        icon_border = user_info.icon_border,  
    }
    return player_info
end

--桌子状态信息
function Desk:getEnterInfo()
    local player_info_list = {}
    for i,palyer in pairs(self.qhb_palyer_list) do
        local info = self:getPlayerInfo(palyer.uid)
        table_insert(player_info_list,info)
    end

    local packet_info = {}
    local info = {
        sender = self:getPlayerInfo(self.sender),
        coins = self.hb_coins,
        thunder_number = self.lei_number,
        count = self.open_count,
        left_count = self.left_count,  
        id = self.hb_id, 
    }
    table_insert(packet_info,info)
    for i,value in ipairs(self.hb_list) do
        local info = {
            sender = self:getPlayerInfo(value.uid),
            coins = value.coins,
            thunder_number = value.lei_number,
            count = value.count,
            left_count = value.count, 
            id = value.id,
        }
        table_insert(packet_info,info)
    end
    local game_status = {
        packet_info = packet_info,
        cur_status = self:getStateNum(), --桌子状态 
        sender_result_coins = self.sender_result_coins,
    }

    local autoInfo = self.autoSendPacketList[uid] or {}

    local enter_info = {
        player_info_list = player_info_list,
        game_status = game_status,
        left_time = self:getLeftTime(),
        table_conf = self:getTableConf(),
        state = self:getStateNum(), --桌子状态 
        room_name = self.room_config:getRoomName(), --桌子名
        left_count = autoInfo.count or 0,
    }

       return {
        game_type = self.game_type,
        table_type = self.table_type,
        hbsl_enter_info = enter_info,
    }
end

function Desk:getTableConf()
    
    local room_data = self.room_config:getRoomData()
    local table_conf = {
        packet_coins_min = room_data.hb_coins_range[1],
        packet_coins_max = room_data.hb_coins_range[2],
        packet_count = room_data.hb_count,
        rate = room_data.hb_times,
    }

    return table_conf
end


function Desk:sendPacket(uid,lei_number,coins,is_robot,group_id)
    local room_data = self.room_config:getRoomData()
    local hb_info = {
        uid = uid,
        coins = coins,
        lei_number = lei_number,
        count = room_data.hb_count,
        is_robot = is_robot,
        id = self.hb_index,
        group_id = group_id,
    }
    self:add_hb_index()
    self.last_lei_number[uid] = lei_number
    self.last_hb_coins[uid] = coins
    table_insert(self.hb_list,hb_info)

    local ret = {
        sender = self:getPlayerInfo(uid),
        coins = coins,
        thunder_number = lei_number,
        count = hb_info.count,
        left_count = hb_info.left_count, 
    }
    --dbglog("sendPacket self.hb_list:",tostring_r(self.hb_list))
    dbglog("sendPacket state",self:getState(),#self.hb_list,self:getLeftTime())
    -- if self:getState() == "rest" and self:getLeftTime() <= 0 then

    --     self:onRest()
    -- end

    return ret
end

function Desk:is_zhong_lei(coins)
    return self.lei_number == coins%10 
end

function Desk:control_zhong_lei()
    local coins = self:suiji()
    local ret_coins = coins
    if not self:is_zhong_lei(coins) then
        local coins_number = coins % 10
        ret_coins = coins + self.lei_number - coins_number
        if ret_coins <= 0 or ret_coins > self.hb_left_coins then
            ret_coins = coins
            errlog("control_zhong_lei error lei_number:",self.lei_number,"left_count:",self.left_count,"ret_coins:",ret_coins)
            ret_coins = coins
        end
    end
    return ret_coins
end

function Desk:control_not_zhong_lei()
    local coins = self:suiji()
    local ret_coins = coins
    if self.left_hb_count == 0 and coins == 1 then
        return coins
    end
    coins = coins == 1 and 2 or coins
    if self:is_zhong_lei(coins) then
        local min = math_min(coins,9)
        ret_coins = coins - math_random(1,min - 1)
        if ret_coins <= 0 or ret_coins > self.hb_left_coins then
            errlog("control_not_zhong_lei error lei_number:",self.lei_number,"left_count:",self.left_count,"ret_coins:",ret_coins)
            ret_coins = coins
        end
    end
    return ret_coins
end

function Desk:suiji()
    local coins = 0
    if self.left_hb_count < 0 then
        errlog("suiji left_count = 0 ")
        return 0
    elseif self.left_hb_count == 0 then
        coins = self.hb_left_coins
        dbglog("suiji coins:",coins,self.left_hb_count,self.hb_left_coins)
    else
       coins = math_ceil((math_random(1,20000)*self.hb_left_coins/(self.left_hb_count+1))/10000)
       if coins == self.hb_left_coins then
            coins = coins - 1
        end
    end
    dbglog("suiji coins:",coins,self.left_hb_count,self.hb_left_coins)
    return coins
end

-- M.SYSTEM_NO_CONTROL = 0 --随机
-- M.SYSTEM_STORE_WIN = 1 --库存1赢钱
-- M.SYSTEM_STORE_LOSE = 2 --库存1输钱
-- M.AWARD_STORE_LOSE = 3 --奖励库存输钱
-- M.PERSONAL_STORE_WIN = 4 --个人库存赢钱
-- M.PERSONAL_STORE_LOSE = 5 --个人库存输钱

-- 抢红包结果控制 返回抢红包金额 和 控制类型
function Desk:control_coins(uid)
    self.left_hb_count = self.left_hb_count - 1
    local store_result, personal_uid 

    if uid == self.sender then
        return self:suiji(),constant.SYSTEM_NO_CONTROL 
    end
    --发红包和抢红包的都是玩家也随机
    if not self.is_robot then
        store_result, personal_uid = self.store_manager:getSystemStoreResultHBSL({uid,self.sender})

        if store_result == constant.PERSONAL_STORE_WIN then
            if uid == personal_uid then
                coins = self:control_not_zhong_lei()
            else
                coins = self:control_zhong_lei()
            end
        elseif store_result == constant.PERSONAL_STORE_LOSE then
            if uid == personal_uid then
                coins = self:control_zhong_lei()
            else
                coins = self:control_not_zhong_lei()
            end
        elseif store_result == constant.SYSTEM_NO_CONTROL then
            return self:suiji(),constant.SYSTEM_NO_CONTROL 
        end  
        return coins,store_result,personal_uid      
    end   
    --store_result 1系统赢 2、系统输 0、随机
    store_result, personal_uid = self.store_manager:getSystemStoreResultHBSL({uid})

    local coins 
    if store_result == constant.PERSONAL_STORE_WIN then
        coins = self:control_not_zhong_lei()
    elseif store_result == constant.PERSONAL_STORE_LOSE then
        coins = self:control_zhong_lei()
    elseif store_result == constant.SYSTEM_NO_CONTROL then
        personal_uid = self.start_personal_uid
        if self.player_lei_count > 0 then
            store_result = self.start_store_result
            coins = self:control_zhong_lei()
            self.player_lei_count = self.player_lei_count - 1
        elseif self.start_store_result == constant.SYSTEM_STORE_LOSE or self.start_store_result == constant.AWARD_STORE_LOSE then
            store_result = self.start_store_result
            coins = self:control_not_zhong_lei()
        else
            store_result = self.start_store_result
            coins = self:suiji()
        end    
    end

    return coins,store_result,personal_uid
end

-- 抢红包结果控制 返回抢红包金额 和 控制类型
function Desk:control_robot_coins()
    self.left_hb_count = self.left_hb_count - 1
    --发红包和抢红包的都是玩家也随机
    if self.is_robot then
        return self:suiji(),constant.SYSTEM_NO_CONTROL 
    end   
    local store_result
    local coins 
    if self.robot_lei_count > 0 then
        coins = self:control_zhong_lei()
        self.robot_lei_count = self.robot_lei_count - 1
        store_result = self.start_store_result
    elseif self.start_store_result == constant.SYSTEM_STORE_WIN or self.start_store_result == constant.PERSONAL_STORE_LOSE then
        store_result = self.start_store_result
        coins = self:control_not_zhong_lei()
    else
        coins = self:suiji()
        store_result = constant.SYSTEM_NO_CONTROL 
    end    

    return coins,store_result
end

--结算机器人抢红包
function Desk:audit_robot()
    table.mix(self.robot_rob_index)
    dbglog(self.robot_rob_index)
    local room_data = self.room_config:getRoomData()
    for _,v in pairs(self.robot_rob_index) do
        local coins,store_result = self:control_robot_coins()
        dbglog("coins:",coins,"left_hb_count:",self.left_hb_count)
        local info = self.qhb_palyer_list[v]
        local uid = info.uid 
        local result_coins = coins
        info.coins = coins
        info.store_result = store_result
        local fee_coins = 0
        if self:is_zhong_lei(coins) then
            result_coins = self.hb_coins*room_data.hb_times - coins
            local ret = self:reduceChip(uid,result_coins,reason.LOSE_COIN)
            info.result_coins = - result_coins
            info.is_thunder = true
            self.lei_count = self.lei_count + 1   --中雷次数+1
        else
            fee_coins = math_floor(coins*room_data.cost/100)
            self:addChip(uid,coins,reason.WIN_COIN)
            self:reduceChip(uid,fee_coins,reason.PAY_FEE)
            self:addFreeChip(uid, fee_coins, reason.PAY_FEE)
            self.win_free_list[uid] = self.win_free_list[uid] or 0
            self.win_free_list[uid] = self.win_free_list[uid] + fee_coins

            info.result_coins = coins - fee_coins
            --对局广播
            self:sendRealHorseMessage(uid,info.result_coins,constant.BROADCAST_IDS.BIDS_HBSL)
            info.is_thunder = false 
        end
        self.hb_left_coins = self.hb_left_coins - coins
        dbglog("self.hb_left_coins:",self.hb_left_coins,"coins:",coins)
        --抢红包记录
        self:addQhbRecord(uid,self.hb_coins,self.lei_number,coins,info.result_coins,self:is_zhong_lei(coins),fee_coins,store_result,true)

        -- --发红包的是玩家，抢红包的是机器人
        -- if not self.is_robot and self.start_personal_uid == 0 then
        --     --更新库存 
        --     local system_money_cost = info.result_coins  
        --     self:updateStore(store_result,system_money_cost,-info.result_coins)
        -- end

        if not self.is_robot then
            self.system_money_cost_a = self.system_money_cost_a + info.result_coins
            self.system_money_cost = self.system_money_cost + info.result_coins + fee_coins          
        end
    end
end

function Desk:robPacket(uid,is_robot)
    --剩余红包次数减1
    self.left_count = self.left_count - 1
    local coins = 0
    local store_result
    local personal_uid
    local info = {}
    --机器人后面结算
    if is_robot then
        info.uid = uid
        --抢红包列表
        table_insert(self.qhb_palyer_list,info)
        table_insert(self.robot_rob_index,#self.qhb_palyer_list)
        dbglog("index:",#self.qhb_palyer_list," uid:",uid)
    else
        coins,store_result,personal_uid = self:control_coins(uid)
        dbglog(uid,"control_coins coins:",coins,"store_result:",store_result,"self.left_count :",self.left_count )

        local room_data = self.room_config:getRoomData()
        local fee_coins = 0
        local user = self.user_manager:getUser(uid) 
        --结算钱
        local result_coins = coins
        info.uid = uid
        info.coins = coins
        info.store_result = store_result
        if self:is_zhong_lei(coins) then
            result_coins = self.hb_coins*room_data.hb_times - coins
            --自己抢自己
            if self.sender == uid then
                self.back_coins = self.back_coins - result_coins
            else
                local ret = self:reduceChip(uid,result_coins,reason.LOSE_COIN)
                if not ret then
                    -- 扣钱失败 返还红包次数
                    self.left_count = self.left_count + 1
                    self.left_hb_count = self.left_hb_count + 1                
                    return false
                end
            end

            info.result_coins = - result_coins
            info.is_thunder = true
            self.lei_count = self.lei_count + 1   --中雷次数+1
        else
            --自己抢自己
            if self.sender == uid then
                self.back_coins = self.back_coins + coins
                info.result_coins = coins
                info.is_thunder = false
            else
                fee_coins = math_floor(coins*room_data.cost/100)

                self:addChip(uid,coins,reason.WIN_COIN)
                self:reduceChip(uid,fee_coins,reason.PAY_FEE)
                self:addFreeChip(uid, fee_coins, reason.PAY_FEE)
                self.win_free_list[uid] = self.win_free_list[uid] or 0
                self.win_free_list[uid] = self.win_free_list[uid] + fee_coins

                info.result_coins = coins - fee_coins
                --对局广播
                self:sendRealHorseMessage(uid,info.result_coins,constant.BROADCAST_IDS.BIDS_HBSL)
                info.is_thunder = false 
            end
        end
        self:checkCaiShen(user, info.result_coins)
        -- --这里先通知每个人自身金币发生变化    
        -- self:ntMoneyChangeToUser(uid)     
        self:ntMoneyChangeToHall(uid, user:getCoins(), constant.MONEY_CHANGED_REASON.HBSL_ROB) 

        self.hb_left_coins = self.hb_left_coins - coins
        dbglog("self.hb_left_coins:",self.hb_left_coins,"coins",coins)
        --抢红包列表
        table_insert(self.qhb_palyer_list,info)
        --抢红包记录
        self:addQhbRecord(uid,self.hb_coins,self.lei_number,coins,info.result_coins,self:is_zhong_lei(coins),fee_coins,store_result,is_robot)

        --发红包和抢红包的不是同一个人
        if self.sender ~= uid then
            --增加流水
            self:winLoseWaterHBSL(user, math_abs(info.result_coins + fee_coins), info.result_coins + fee_coins,fee_coins,0)
            if self.is_robot then
                self.system_money_cost_a = self.system_money_cost_a - (info.result_coins)
                self.system_money_cost = self.system_money_cost - (info.result_coins + fee_coins)    
            end

            --把输赢发送到好友服
            self:sendLoseWinToFriendServer(-info.result_coins) 

            local bet_coins = 0
            local produce = info.result_coins + fee_coins
            local win_lose = info.result_coins 
            user:addProduce(produce)
            self:sendSummary(uid, bet_coins, produce, win_lose, fee_coins)    
        end

        --发红包的是玩家，抢红包的是玩家
        if not self.is_robot and  not is_robot then
            if personal_uid == uid and uid ~= self.sender then
                --更新库存  
                self:updateStorePersonal(store_result,info.result_coins + fee_coins,personal_uid)           
            end
        elseif self.is_robot and not is_robot then
            --更新库存  
            self:updateStoreEX(store_result,-(info.result_coins+ fee_coins),info.result_coins + fee_coins,personal_uid)           
        end    
    end

    --红包已经抢完了  极端情况这里已经跳转到等待结束
    if self.left_count <= 0 and self:getState() == "startgame" then
        dbglog("left_count 0")
        self:stopTimer()
        self:changeState("to_waitover")
    end

    return true,info.coins,#self.qhb_palyer_list,self:is_zhong_lei(coins)
end

function Desk:checkPacketNum( ... )
    --红包已经抢完了  极端情况这里已经跳转到等待结束
    if self.left_count <= 0 and self:getState() == "startgame" then
        dbglog("____________left_count 0")
        self:changeState("to_waitover")
    end    
end

function Desk:canRobPacket(uid,curr_coins)
    local room_data = self.room_config:getRoomData()

    for _,v in pairs(self.qhb_palyer_list) do
        if v.uid == uid then
            return false,error_code.ROB_PACKET_FULL
        end
    end    

    if curr_coins < self.hb_coins*room_data.hb_times then
        return false,error_code.GOLD_IS_NOT_ENOUGH
    end

    if self.left_count <= 0 then
        return false,error_code.HONG_BAO_IS_EMPTY
    end

    return true
end

function Desk:UpdateAutoSendPacket(uid)
    local autoInfo = self.autoSendPacketList[uid]
    if autoInfo == nil then return end

    if autoInfo.group_id and self:getHbCountByGroup(autoInfo.group_id) ~= 0 then
        return
    end

    --去扣钱
    local ret = self:reduceChip(uid, autoInfo.coins, reason.BET_COIN)    
    if not ret then
        self.autoSendPacketList[uid] = nil 
        dbglog(uid,"金币不足，金币发红包功能已经关闭")
        self:sendToUser(uid, 'table.NTF_TIPS', {type = 0, content = "金币不足，金币发红包功能已经关闭" })
        self:sendToUser(uid,'hbsl.NTF_AUTO_SEND',{left_count = 0})
        return 
    end

    --这里先通知每个人自身金币发生变化
    self:ntMoneyChangeToUser(uid)        
    self:ntMoneyChangeToHall(uid, ret, constant.MONEY_CHANGED_REASON.HBSL_SEND)   

    if autoInfo.count then
        autoInfo.count = autoInfo.count - 1
        self:sendToUser(uid,'hbsl.NTF_AUTO_SEND',{left_count = autoInfo.count})
        if autoInfo.count == 0 then
            self.autoSendPacketList[uid] = nil 
        end
        self:sendPacket(uid,autoInfo.lei_number,autoInfo.coins,false,autoInfo.group_id)
        return
    end
   

    self:sendPacket(uid,autoInfo.lei_number,autoInfo.coins,false)
end

--添加添加自动发红包
function Desk:autoSendPacket(uid,count,coins,lei_number)
    if not lei_number then
        lei_number = math_random(0,9)
    end 
    self.autoSendPacketList[uid] = {
        uid = uid,
        count = count,
        coins = coins,
        lei_number = lei_number,
        group_id = self.group_index,
    }
    self:UpdateAutoSendPacket(uid)
    self:add_group_index()
end

--撤销自动发红包
function Desk:revokeAutoSendPacket(uid)
    self.autoSendPacketList[uid] = nil 
end

--无限发红包
function Desk:InfiniteSendPacket(uid)
    local lei_number = self.last_lei_number[uid]
    if not lei_number then
        lei_number = math_random(0,9)
    end 
    local coins = self.last_hb_coins[uid]
    if not coins then
        local room_data = self.room_config:getRoomData()
        coins = math_random(room_data.hb_coins_range[1],room_data.hb_coins_range[2])
    end

    self.autoSendPacketList[uid] = {
        uid = uid,
        coins = coins,
        lei_number = lei_number,
    }  
end

--获取红包列表
function Desk:getPacketInfo(uid,param)
    local packet_info = {}
    for i,value in pairs(self.hb_list) do
        if param ~= 1 or value.uid == uid then
            local info = {
                sender = self:getPlayerInfo(value.uid),
                coins = value.coins,
                thunder_number = value.lei_number,
                count = value.count,
                left_count = value.count, 
                id = value.id,
                group_id = value.group_id,
            }
            table_insert(packet_info,info)
        end
    end

    return packet_info
end

--删除红包
function Desk:delPacket(uid) 
    local i = 1 
    while i <= #self.hb_list do
        local item = self.hb_list[i]
        if item.uid == uid then
            dbglog("delPacket",uid,tostring_r(item))
            --返回钱
            if not item.is_robot then
                self:addChip(uid,item.coins,reason.FHB_BACK)
            end
            table_remove(self.hb_list,i)
        else
            i = i + 1
        end
    end 
    --这里先通知每个人自身金币发生变化
    local user = self.user_manager:getUser(uid)
    self:ntMoneyChangeToUser(uid)   
    if not user then return end     
    self:ntMoneyChangeToHall(uid, user:getCoins(), constant.MONEY_CHANGED_REASON.HBSL_SEND)     
end

--撤销红包
function Desk:revokePacket(uid,packet_id,group_id)
    local i = 1 
    while i <= #self.hb_list do
        local item = self.hb_list[i]
        dbglog("revokePacket",uid,packet_id,tostring_r(item))
        if item.uid == uid and (((packet_id == 0 and group_id == 0) or item.id == packet_id) or (group_id ~= 0 or item.group_id == group_id) )then
            --返回钱
            if not item.is_robot then
                self:addChip(uid,item.coins,reason.BET_COIN_BACK)
            end
            table_remove(self.hb_list,i)
        else
            i = i + 1
        end
    end

    if group_id ~= 0  and packet_id == 0 then
        self.autoSendPacketList[uid] = nil 
        dbglog("NTF_AUTO_SEND eft_count = 0")
        self:sendToUser(uid,'hbsl.NTF_AUTO_SEND',{left_count = 0})
    end

   local packet_info = {}
    for i,value in pairs(self.hb_list) do
        local info = {
            sender = self:getPlayerInfo(value.uid),
            coins = value.coins,
            thunder_number = value.lei_number,
            count = value.count,
            left_count = value.count, 
            id = value.id,
        }
        table_insert(packet_info,info)
    end

    --这里先通知每个人自身金币发生变化
    local user = self.user_manager:getUser(uid)
    local info = self:revokeList(uid,user)
    self:ntMoneyChangeToUser(uid)        
    self:ntMoneyChangeToHall(uid, user:getCoins(), constant.MONEY_CHANGED_REASON.HBSL_SEND) 

    return packet_info,info
end

--增加抢红包记录
function Desk:addQhbRecord(uid,coins,num,coin,win_lose,is_lei,fee_coins,store_result,is_robot)
    local user = self.user_manager:getUser(uid)
    if not user then return end 
    local info = {
        uid = uid,
        rob_coins = coin,
        is_lei = is_lei,
        result_coins = win_lose,
        fee_coins = fee_coins,    
        store_result = store_result,
        is_robot = user:isRobot(),
    }
    if uid ~= self.sender then
        table_insert(self.player_list,info)
        self.total_num = self.total_num + 1
    else
        self.player_list[1] = info
    end

    if not is_robot then
        local server = R().basesvr({key=uid})
        server:send('.msg_handler', 'add_qhb_record', uid,coins,num,coin,win_lose,is_lei) 
    end
    
    user:add_qhb_record(uid,coins,num,coin,win_lose,is_lei)
end

--增加发红包记录
function Desk:addFhbRecord(uid,coins,hit_player_count,win_lose,fee_coins,is_robot)
    local info = self.player_list[1]
    info.result_coins = win_lose - fee_coins
    info.fee_coins = (info.fee_coins or 0) + fee_coins
    if not is_robot then
        local server = R().basesvr({key=uid})
        server:send('.msg_handler', 'add_fhb_record',uid,coins,hit_player_count,win_lose)
    end
    local user = self.user_manager:getUser(uid)
    if not user then return end 
    user:add_fhb_record(uid,coins,hit_player_count,win_lose)
end

--库存处理
--control_coins 玩家输赢币
--system_money_cost 系统输赢
--control_result 系统控制结果
--control_uid 受控玩家ID
function Desk:updateStore(system_result,control_coins,control_uid)
    dbglog(system_result,"control_uid:",control_uid,"system_money_cost",self.system_money_cost,"control_coins:",control_coins)
    local system_send_award = system_result == constant.AWARD_STORE_LOSE
    local gm_store_coins = 0 --个人控制结果金币的输赢
    --更新个人库存
    if control_uid then 
        gm_store_coins = self.store_manager:updatePersionStore(self.system_money_cost,control_coins, control_uid,system_result )
    end

    --更新系统库存
    self.store_manager:updateSystemStore(self.system_money_cost - gm_store_coins, system_send_award)
    dbglog("________updateSystemStore_________", self.system_money_cost, gm_store_coins ,system_send_award)
    --更新抽水库存,奖励库
    self.store_manager:updateRobotStore(math_abs(self.system_money_cost) - math_abs(gm_store_coins))
    self.store_manager:checkStoreTransfer()
end

--发红包的是机器人 抢红包的是玩家
function Desk:updateStoreEX(system_result,system_money_cost,control_coins,control_uid)
    dbglog(system_result,"control_uid:",control_uid,"system_money_cost",system_money_cost,"control_coins:",control_coins)
    local system_send_award = system_result == constant.AWARD_STORE_LOSE
    local gm_store_coins = 0 --个人控制结果金币的输赢
    --更新个人库存
    if control_uid then 
        gm_store_coins = self.store_manager:updatePersionStore(system_money_cost,control_coins, control_uid,system_result )
    end

    --更新系统库存
    self.store_manager:updateSystemStore(system_money_cost - gm_store_coins, system_send_award)
    dbglog("________updateSystemStore_________", system_money_cost, gm_store_coins ,system_send_award)
    --更新抽水库存,奖励库
    self.store_manager:updateRobotStore(math_abs(system_money_cost) - math_abs(gm_store_coins))
    self.store_manager:checkStoreTransfer()
end

--更新个人控制
function Desk:updateStorePersonal(control_coins, control_uid,system_result) 
    --更新个人库存
    if control_uid then 
        -- gm_store_coins = self.store_manager:updatePersionStoreHBSL(control_coins, control_uid,system_result )
        gm_store_coins = self.store_manager:updatePersionStore(control_coins, control_coins, control_uid,system_result )
    end
end

--牌局记录
function Desk:writeCardRecord()
    local side_list = {} --池信息
    local user_list = self.user_manager:getRealBetUsers() --下过注的真人
    local side_list = {}
    local player_win = 0
    local win_list = {}
    local lose_list = {}
    for i,v in pairs(self.player_list) do
        local user = self.user_manager:getUser(v.uid)

        if v.result_coins >= 0 then
            --增加赢的局数
            user:addWinCount()  
            user:setLatelyWin(true) --最近20局 
            --输赢记录列表
            if user:isPlayer() then 
                table_insert(win_list, {uid = v.uid, left_score = user:getCoins(), add_score = v.result_coins, 
                    pay_fee = self.win_free_list[v.uid] , is_robot = user:isRobot()})
                player_win = player_win + v.result_coins
            end
        elseif v.result_coins < 0 then
            --输赢记录列表
            if user:isPlayer() then 
                table_insert(lose_list, {uid = v.uid, left_score = user:getCoins(), add_score = v.result_coins, 
                    pay_fee = self.win_free_list[uid], is_robot = user:isRobot()})
                player_win = player_win + v.result_coins
            end
        end
    end


    local table_gid = os.date("%Y%m%d%H%M%S") .."_".. self.table_id
    --system_result     
    local record = {
              exinfo = {
                system_result= 0, --系统控制类型
                control_uid= 0, 
                sender = self.sender,
                lei_number = self.lei_number,
                hb_coins = self.hb_coins,
              },
              end_time = self.end_time,
              begin_time = self.start_time,
              total_num = self.total_num, --参加游戏人数
              table_gid = table_gid,
              game_type = self.game_type,
              address = skynet.address(skynet.self()),
              table_type = self.table_type,
              loser_list = lose_list,
              winner_list = win_list,
              side_list= self.player_list, --详情
              system_win = player_win,

              op= "card_record",          
    }
    billlog(record)

    dbglog("___side_list__1111111111111111111___________",dump(record))
end

--通知财神
function Desk:caishen_time()    
    print("=============notify_caishenjiadao++++++++++++++++++++")
    local curr_time = util.get_now_time()
    local caishenjiadao = self.room_config:getCaiShenJiaDao()
    local countdown_time = caishenjiadao.countdown_time or 10
    local end_time = curr_time + countdown_time + 2 --把动画时间加上
    if self:getState() == "startgame" then
        end_time = curr_time + countdown_time + self:getLeftTime() + self:getStateTime("waitover") + 1 --把动画时间加上 
        print("=============notify_caishenjiadao--------------------")
    end
    return end_time
end

function Desk:getFhbRecord(uid)
    local user = self.user_manager:getUser(uid)
    local server = R().basesvr({key=uid})
    local ok,fhb_record = server:call('.msg_handler', 'get_fhb_record',uid)

    local total_coins = 0
    local total_thunder_player = 0
    local send_list = {}
    for i = 1 , #fhb_record do
        local idx = #fhb_record - i + 1
        local item = fhb_record[idx]
        table_insert(send_list,{coins = item.coins,thunder_player_count = item.hit_count})
        total_coins = total_coins + item.coins
        total_thunder_player = total_thunder_player + item.hit_count
        if i == 10 then break end
    end
    return {
        total_coins = total_coins,
        total_thunder_player = total_thunder_player,
        send_list = send_list,
    }
end

function Desk:getQhbRecord(uid)
    local user = self.user_manager:getUser(uid)
    local server = R().basesvr({key=uid})
    local ok,qhb_record = server:call('.msg_handler', 'get_qhb_record',uid)

    local rob_list = {}
    local lei_count = 0
    local total_coins = 0
    local rob_total_coins = 0
    for i = 1 , #qhb_record do
        local idx = #qhb_record - i + 1
        local item = qhb_record[idx]
        if item.is_lei then
            lei_count = lei_count + 1
        end
        total_coins = total_coins + item.coins

        -- if item.win_lose > 0 then
            rob_total_coins = rob_total_coins + item.win_lose
        -- end

        table_insert(rob_list,{
            coins = item.coins,
            thunder_number = item.num,
            rob_coins = item.win_lose,
            is_thunder = item.is_lei,
            })
        if i == 10 then break end
    end
    -- dbglog(rob_list)
    return {
        rob_list = rob_list,
        total_thunder_player = lei_count,
        total_coins = total_coins,
        rob_total_coins = rob_total_coins,
    }
end

--客户端需要返回的个人信息
function Desk:getHbslClientInfo(user)
    local qhb_record = user:getQhbRecord()
    local fhb_record = user:getFhbRecord()

    local coins = 0 
    for _,v in pairs(fhb_record) do
        coins = coins + v.coins
    end

    local miss_count = 0 
    for _,v in pairs(qhb_record) do
        if v.is_lei == false then
            miss_count = miss_count + 1
        end
    end

    return {
        uid = user:getUid(),
        name = user:getName(),
        coins = user:getCoins(),
        icon = user:getIcon(),
        vip_level = user:getVipLevel(),
        icon_border = user:getIconBorder(),
        total_bet = coins,
        win_count = miss_count, --最近20局
        play_count = #qhb_record, --最近20局
    }
end

function Desk:getHbCountByGroup(group_id)
    local count = 0
    for _,v in pairs(self.hb_list) do
        if v.group_id == group_id then
            count = count + 1
        end
    end
    return count
end

function Desk:revokeList(uid,user)
    local ret_info = {}
    local group_id 
    local auto = self.autoSendPacketList[uid]
    if auto and auto.count and auto.count > 0 then
        local item = {
            count = auto.count + self:getHbCountByGroup(auto.group_id),
            group_id = auto.group_id,
            coins = auto.coins,
            name = user:getName(),
        }
        group_id = auto.group_id
        table_insert(ret_info, item)
    end

    for _,v in pairs(self.hb_list) do
        if v.uid == uid and (not group_id or v.group_id ~= group_id) then
             local item = {
                name = user:getName(),
                count = 1,
                group_id = v.group_id,
                coins = v.coins,
                packet_id = v.id,
            }
            table_insert(ret_info, item)
        end
    end

    return ret_info
end

return Desk
